package com.cyou.qb.common.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.cyou.nad.datastore.query.JdbcPage;
import com.cyou.nad.datastore.query.JdbcPageDao;
import com.cyou.qb.common.dao.AssignedWinnerDao;
import com.cyou.qb.common.dao.WinnerDao;
import com.cyou.qb.common.domain.Winner;
import com.cyou.qb.common.domain.WinnerContract;
import com.cyou.qb.common.domain.WinnerEx;
import com.cyou.qb.common.service.WinnerService;

@Service("WinnerServiceImpl")
public class WinnerServiceImpl extends BaseSerivceImpl implements WinnerService {

	@Autowired
	private AssignedWinnerDao assingedWinnerDao;

	@Autowired
	private WinnerDao winnerDao;
	@Autowired
	private JdbcPageDao jdbcPageDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Winner> findWinner(int awardId, int activeId, Date startTime, Date endTime) {
		return winnerDao.findWinnerByConditions(activeId, awardId, startTime, endTime);
	}

	@Override
	public void saveOneWinner(Winner winner) {
		winnerDao.addOneWinner(winner);
		//this.getServiceContext().addLog(winner.getPassport(), -1, "t_winner", "添加中奖记录", new Object[]{winner}, null);
	}

	public AssignedWinnerDao getAssingedWinnerDao() {
		return assingedWinnerDao;
	}

	public void setAssingedWinnerDao(AssignedWinnerDao assingedWinnerDao) {
		this.assingedWinnerDao = assingedWinnerDao;
	}

	public WinnerDao getWinnerDao() {
		return winnerDao;
	}

	public void setWinnerDao(WinnerDao winnerDao) {
		this.winnerDao = winnerDao;
	}

	@Override
	public List<WinnerEx> findTopWinnerByActive(int activeId, int size) {
		String sql = "\n select a.id, a.active_id,a.award_id,a.award_detail_id,a.u_center_id,a.passport,a.nickname,a.win_time,b.name from t_winner a ,t_award b"
				+ "\n where a.award_id = b.id " + "\n and  a.active_id =? order  by win_time desc  limit ?";
		List<WinnerEx> list = jdbcTemplate.query(sql, new Object[] { activeId, size }, new RowMapper<WinnerEx>() {
			@Override
			public WinnerEx mapRow(ResultSet rs, int rowNum) throws SQLException {
				WinnerEx ex = new WinnerEx();
				ex.setId(rs.getInt("id"));
				ex.setActiveId(rs.getInt("active_id"));
				ex.setAwardId(rs.getInt("award_id"));
				ex.setAwardDetailId(rs.getInt("award_detail_id"));
				ex.setuCenterId(rs.getInt("u_center_id"));
				ex.setPassport(rs.getString("passport"));
				ex.setNickname(rs.getString("nickname"));
				ex.setWinTime(rs.getTimestamp("win_time"));
				ex.setAwardName(rs.getString("name"));
				return ex;
			}
		});
		return list;
	}

	@Override
	public JdbcPage pageWinnerDraw(int uCenterId, int pageNo, int pageSize) {
		String sql = "select aaa.*,bbb.card_no cardNo,bbb.card_pwd cardPwd from ("
				+ "select aa.*,bb.address,bb.real_name realName from("
				+ "select image,b.memo,b.url,b.name awardName,b.type,c.id activeId,c.name activeName,a.id winnerId,a.award_id awardId,"
				+ "a.award_detail_id awardDetailId,a.win_time winTime from t_winner a,t_award b,"
				+ "t_active c where a.award_id=b.id and a.active_id = c.id and a.u_center_id = ? and b.type <= 2"
				+ ")aa left join t_winner_contract bb on aa.type=1 and bb.winner_id = aa.winnerid"
				+ ") aaa left join t_award_detail bbb on aaa.type=2 and awardDetailId=bbb.id "
				+ "order by winTime DESC";
		return jdbcPageDao.pageQuery(sql, pageNo, pageSize, new Object[] { uCenterId });
	}

	@Override
	public int saveWinnerContract(WinnerContract contract) {
		return winnerDao.saveWinnerContract(contract);
	}
}
